const wasm_loader = require('wasm_loader')
export class libInterface
{

    InitLibrary()
    {
        console.log(Game.time+"调用初始化 wasm module ");
        //
        let _this = this;
        wasm_loader('LayoutCal_mod', 'LayoutCal').then((instance) => {
            console.log("WASM module loaded.");
            _this.mod = instance;
            global.iswasmLoad = true;
            //  _this.init_portal();
        });
    }
    GetTerrainLayout(terrainjson)
    {
        // console.log("GetTerrainLayout 初始化:",(terrainjson));
        if(this.mod!=undefined)
        {
            console.log("GetTerrainLayout:",(terrainjson));
            let val =    this.mod.GetTerrainLayout(terrainjson);
            console.log("buffer",val);
            return val;
        }
        return null;
    }
    GetTerrainModuleWithCenter(terrainjson,x,y)
    {
        // console.log("GetTerrainLayout 初始化:",(terrainjson));
        if(this.mod!=undefined)
        {
            console.log("GetTerrainModuleWithCenter:",(terrainjson));
            let val =    this.mod.GetTerrainModuleWithCenter(terrainjson,x,y);
            console.log("buffer",val);
            return val;
        }
        return null;
    }
    GetTerrainModule(terrainjson)
    {
        // console.log("GetTerrainLayout 初始化:",(terrainjson));
        if(this.mod!=undefined)
        {
            console.log("GetTerrainModule:",(terrainjson));
            let val =    this.mod.GetTerrainModule(terrainjson);
            console.log("buffer",val);
            return val;
        }
        return null;
    }
    hash(instr)
    {
        // console.log("GetTerrainLayout 初始化:",(terrainjson));
        if(this.mod!=undefined)
        {

            let val =    this.mod.hash(instr);

            return val;
        }
        return null;
    }

    init_portal()
    {
        if(this.mod!=undefined)
        {

            let val =    this.mod.InitPortal();

            return val;
        }
        return null;
    }
    is_portal_inited()
    {
        if(this.mod!=undefined)
        {

            let val =    this.mod.IsPortalInit();

            return val;
        }
        return null
    }
    find_portal_route(from,to)
    {
        if(this.mod!=undefined)
        {
            if(this.is_portal_inited())
            {
                let val =    this.mod.FindPortalRoute(from.shard,from.room,to.shard,to.room);

                return JSON.parse(val);
            }
            else
            {
                console.log(`星门地图还未初始化!无法寻路`);
            }
        }
        return null;
    }

    getSafeRegion(terrain,rampart,center)
    {
        if(this.mod!=undefined)
        {

            let val =    this.mod.GetSafeRegion(terrain,rampart,center);

            return JSON.parse(val);
        }
        return null;
    }
}
